//
//  SettingViewController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//
//  设置与帮助主见面


#import "SettingViewController.h"
#import "FeedbackController.h"
#import "LeftViewController.h"
#import "HomeViewController.h"
#import "SVProgressHUD.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>

// tableView
@property (nonatomic,strong) UITableView *tableView;

// 数据
@property (nonatomic,strong) NSArray *dataArray;



@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    
    self.dataArray = @[@"意见反馈",@"加号符号说明",@"修改密码",@"注销用户"];
    
    [self setupUI];
}


- (void)setupUI{
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.top.mas_equalTo(0);
    } ];
    
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}


#pragma mark
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIViewController *vc;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0: // 意见与反馈
                vc = [(FeedbackController *)[FeedbackController alloc]init];
                break;
            case 1: //加号符号说明
                
                break;
            case 2://修改密码
                
                break;
                
            case 3: // 注销(弹出提示框)
                [self cancelAccount];
                break;
                
            default:
                break;
        }
        // 导航栏标题
        vc.title = self.dataArray[indexPath.row];
    }
    
    // 跳转
    if (indexPath.row != 3) {
        [self jumpToOtherViewController:vc];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)cancelAccount{
    
    NSNumber *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    
    if (isLogin.boolValue) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定注销吗?" preferredStyle:UIAlertControllerStyleActionSheet];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            // 1.leftview 中切换 headView
            self.cancelSuccess();
            
            // 2.更改沙盒中的登录状态
            [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:@"isLogin"];
            
            // 2.显示遮罩(用第三方框架)
            [SVProgressHUD showSuccessWithStatus:@"注销成功!"];//显示
            [SVProgressHUD setFadeInAnimationDuration:0];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            [SVProgressHUD dismissWithDelay:1];
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            // 点击响应事件
        }]];
        
        // 弹出提示框
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        [SVProgressHUD showErrorWithStatus:@"当前没有登录"];//显示
        [SVProgressHUD setFadeInAnimationDuration:0];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD dismissWithDelay:1];
        
    }
    
    
   
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        // 设置代理
        _tableView.dataSource =self;
        _tableView.delegate = self;
    }
    return _tableView;
}

@end
